/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Collections.Generic;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreLocation;
using System.Drawing;

namespace ArtNotes
{
	/// <summary>
	/// Uses either the devices camera or photolibrary
	/// to get an image to be imported into the app.
	/// Rather than performing the save etc in this
	/// class, it should (is) instead handled by
	/// the calling view controller.
	/// </summary>
	public class ANImportImageFromDevice : UIImagePickerController
	{

		private CLLocationManager locationManager;
		//private Tag tag;
		private ANDatabase anDB;
		
		private UIImage importedImage;
		public UIImage ImportedImage
		{
			get { return importedImage; }
		}
		
		public ANImportImageFromDevice (IntPtr p) : base(p)
		{
		}
		
		public ANImportImageFromDevice (bool camera)//, Tag tag)
		{
			//this.tag = tag;
			locationManager = AN_APP_OPTIONS.AN_LOCATION;
			anDB = AN_APP_OPTIONS.AN_DATABASE;
			
			if(camera)
				this.SourceType = UIImagePickerControllerSourceType.Camera;
			else
				this.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
			
			this.Delegate = new ImagePickerDelegate(this);
		}
	
		class ImagePickerDelegate : UIImagePickerControllerDelegate
		{
			ANImportImageFromDevice imageFromDevice;
			
			public ImagePickerDelegate (ANImportImageFromDevice imageFromDevice)
			{
				this.imageFromDevice = imageFromDevice;
			}
			
			public override void FinishedPickingImage (UIImagePickerController picker, UIImage image, NSDictionary editingInfo)
			{
				//UIActivityIndicatorView pind = new UIActivityIndicatorView (new RectangleF (265f, 12f, 40f, 40f)){
				//	ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray,
				//	AutoresizingMask = UIViewAutoresizing.FlexibleLeftMargin |
				//		UIViewAutoresizing.FlexibleRightMargin |
				//		UIViewAutoresizing.FlexibleTopMargin |
				//		UIViewAutoresizing.FlexibleBottomMargin
				//};
	
				//pind.StartAnimating ();
				//pind.SizeToFit ();
				//picker.NavigationItem.RightBarButtonItem.CustomView = pind;
				
				//this.saveImage(image);
				
				imageFromDevice.importedImage = image;
				picker.DismissModalViewControllerAnimated(true);
			}
			
			public override void Canceled (UIImagePickerController picker)
			{
				imageFromDevice.importedImage = null;
				//base.Canceled (picker);
				picker.DismissModalViewControllerAnimated(true);
			}

				
			/*private void saveImage(UIImage image)
			{				
				string randomName = Path.ChangeExtension(Path.GetRandomFileName(), ".png");
				Image img = new Image();
				
				NSError err= null;
				NSData imgData = image.AsPNG();
				imgData.Save(AN_APP_OPTIONS.DOC_DIR + randomName, false, out err);
				img.FileName = randomName;
				img.Title = "Imported Image";
				img.Description = DateTime.Now.ToString();
				if(imageFromDevice.locationManager != null)
				{
					img.Lattitude = imageFromDevice.locationManager.Location.Coordinate.Latitude;
					img.Longitude = imageFromDevice.locationManager.Location.Coordinate.Longitude;
				}
				
				Tag[] tags = new Tag[1];
				tags[0] = imageFromDevice.tag;
				
				this.imageFromDevice.anDB.CreateImage(img,tags);
			}*/
		}
	}
}
